package service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import common.impl.exception.ServiceException;

import model.MUser;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import service.interf.IUser;
import dao.interf.IUserDAO;
import dao.pojo.User;

public class UserImpl implements IUser {

	private IUserDAO userDao;
	
	public void setUserDao(IUserDAO userDao) {
		this.userDao = userDao;
	}

	/**
	 * 删除用户
	 * @param
	 */
	public String delUser(Integer userId) throws Exception {
		userDao.delById(userId);
		return "1";
	}

	public String delUser(String userName) throws Exception {
		User user = null;
		List<User> userLst = userDao.findByProperties(new String[]{"userName"}, 
				new Object[]{userName});
		if(!userLst.isEmpty()) {
			user = userLst.get(0);
		}
		if(null == user) {
			throw new ServiceException("ERR-");
		}
		userDao.del(user);
		return "1";
	}

	/**
	 * 编辑用户
	 * @param MUser
	 * @return "1"
	 *         成功
	 * @throws Exception
	 */
	public String editUser(MUser model) throws Exception {
		Integer userId = model.getUserId();
		if(null == userId) {
			throw new ServiceException("ERR-");
		}
		
		User user = userDao.findById(userId);
		if(null == user) {
			throw new ServiceException("ERR-");
		}
		String username = model.getUserName();
		Integer uId = model.getUserId();
		String password = model.getPassword();
		Short enabled = model.getEnabled();
		//注入user
		if(!StringUtils.isEmpty(username)) {
			user.setUserName(username);
		}
		if(!StringUtils.isEmpty(password)) {
			user.setPassword(password);
		}
		if(null != uId) {
			user.setUserId(uId);
		}
		if(null != enabled) {
			user.setEnabled(enabled);
		}
		userDao.update(user);
		return "1";
	}

	/**
	 * 返回所有用户
	 * @param
	 *     分页信息：暂时不用
	 * @return JSONArray
	 * @throws Exception
	 */
	public String findAll(int pageno, int pagesize) throws Exception {
		JSONArray jarr = new JSONArray();
		List<User> userLst = userDao.findAll();
		if(!userLst.isEmpty()) {
			for(User u : userLst) {
				JSONObject jobj = new JSONObject();
				jobj.put("userId", u.getUserId());
				jobj.put("userName", u.getUserName());
				jobj.put("loginTime", u.getLoginTime());
				jobj.put("loginNums", u.getLoginNums());
				jobj.put("password", u.getPassword());
				jobj.put("enabled", u.getEnabled());
				jarr.add(jobj);
			}
		}
		JSONObject jret = new JSONObject();
		jret.put("rows", jarr);
		return jret.toString();
	}

	public String addUser(MUser model) throws Exception {
		User user = new User();
		String username = model.getUserName();
		String password = model.getPassword();
		Short enabled = model.getEnabled();
		//注入user
		if(!StringUtils.isEmpty(username)) {
			user.setUserName(username);
		}
		if(!StringUtils.isEmpty(password)) {
			user.setPassword(password);
		}
		if(null != enabled) {
			user.setEnabled(enabled);
		}
		//获取当前系统的时间
		user.setLoginTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		userDao.save(user);
		return "1";
	}

	/**
	 * 通过用户编号查找用户
	 */
	public String findUserById(Integer userId) throws Exception {
		JSONObject jobj = new JSONObject();
		if(userId != null) {
			User u = userDao.findById(userId);
			jobj.put("userId", u.getUserId());
			jobj.put("userName", u.getUserName());
			jobj.put("loginTime", u.getLoginTime());
			jobj.put("loginNums", u.getLoginNums());
			jobj.put("enabled", u.getEnabled());
		}
		
		return jobj.toString();
	}

	/**
	 * 通过用户名查找用户
	 */
	public String findUserByUserName(String userName) throws Exception {
		JSONObject jobj = new JSONObject();
		if(userName != null) {
			User u = userDao.findByProperties(new String[] {"userName"},
					new Object[] { userName }).get(0);
			jobj.put("userId", u.getUserId());
			jobj.put("userName", u.getUserName());
			jobj.put("loginTime", u.getLoginTime());
			jobj.put("loginNums", u.getLoginNums());
			jobj.put("enabled", u.getEnabled());
		}
		return jobj.toString();
	}
	
	/**
	 * 使能用户
	 * 
	 * @param uname
	 *            :用户名
	 * @param enabled
	 *            :0 禁用 1启用
	 */
	public String enableUser(String uname, Short enabled) throws Exception {
		User user = userDao.findByProperties(new String[]{"userName"}, 
				new Object[] {uname}).get(0);
		user.setEnabled(enabled);
		user = userDao.update(user);
		return user.getUserName();
	}
	/**
	 * 使能用户
	 * 
	 * @param userId
	 *            :用户编号
	 * @param enabled
	 *            :0 禁用 1启用
	 */
	public String enableUser(Integer userId, Short enabled) throws Exception {
		User user = (User) userDao.findById(userId);
		user.setEnabled(enabled);
		user = userDao.update(user);
		return user.getUserName();
	}
	
	
	/**
	 * 修改用户密码
	 * 
	 * @param uname
	 *            用户名
	 * @param oldpwd
	 * @param 旧密码
	 * @param newpwd
	 *            新密码
	 * @return 失败错误编号
	 * @throws Exception
	 */
//	public String changePwd(Long userId, String oldpwd, String newpwd)
//			throws Exception {
//		User user = null;
//		user = (User) userDao.findById(userId);
//		if (null == user)
//			throw new ServiceException("ERR-0011");
//		String psw = MD5Transform.EncoderByMd5(oldpwd, "utf-8");
//		if (!psw.equals(user.getPassword()))
//			throw new ServiceException("ERR-0019");
//		user.setPassword(MD5Transform.EncoderByMd5(newpwd, "utf-8"));
//		userDao.update(user);
//		return "1";
//	}
	
}
